<!DOCTYPE html>
<%@ page import="java.util.*,rubix.model.*,rubix.controller.*,java.text.*" %>
<%
    String username = "";
    if (session.getAttribute("loggedIn") != null) {
        username = (String) session.getAttribute("loggedIn");
        if (username.equals("irvin.tan")) {
            response.sendRedirect("adminMenu.jsp");
        } else {
            response.sendRedirect("index.jsp");
        }   
    }
    String loggedOut = (String) session.getAttribute("loggedOut");
    String errorMsg = (String) session.getAttribute("errorMsg");
    
    ArrayList<String> departmentList = UserManager.retrieveUniqueDepartment();

%>

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>KMP Far East Pte Ltd - ERP Application</title>

        <!-- Bootstrap core CSS -->
        <link href="css/bootstrap.css" rel="stylesheet">
        <!-- Add custom CSS here -->
        <link href="css/sb-admin.css" rel="stylesheet">
        <link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
        <!-- Page Specific CSS -->
        <link rel="stylesheet" href="http://cdn.oesmith.co.uk/morris-0.4.3.min.css">
    </head>

    <body>

          <div id="login-wrapper">

            <!-- Sidebar -->
            <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index.jsp">KMP Far East Pte Ltd - ERP Application</a>
                </div>
            </nav>
            
            <form method="POST" action="logInOutController" accept-charset="UTF-8">
                <%  
                    if (loggedOut != null) {
                %>
                <div class="alert alert-success">
                    <a class="close" data-dismiss="alert" href="#">x</a>
                    <%=loggedOut%><%session.removeAttribute("loggedOut");%>
                </div>

                <%
                }

                if (request.getSession(false) != null && session.getAttribute("loggedIn") != null) {
                    Boolean b = (Boolean)session.getAttribute("isAdmin");
                    if (b){
                        response.sendRedirect("adminMenu.jsp");
                    } else{ 
                        response.sendRedirect("index.jsp");
                    }
                }

                if (errorMsg != null) {
                    %><div class="alert alert-error">
                <a class="close" data-dismiss="alert" href="#">x</a>
                <%=errorMsg%>
                <%session.removeAttribute("errorMsg");%>
                </div>
                <%
                }
                %>                
                <div id="page-wrapper" class="container" style="margin-left:0px">
                    <div class="col-lg-7">
                        <div class="panel panel-primary">
                            <div class="panel-heading">
                                <h1 class="panel-title">Login</h1>
                            </div>
                            <div class="panel-body"> 
                                <div class="form-group">                                   
                                    <label>Username</label>
                                    <input class="form-control" name="username" placeholder="Enter Username"><br>
                                    <label>Password</label>
                                    <input class="form-control" name="password" placeholder="Enter Password" type="password"><br>
                                    <label>Department</label>
                                    <select name="department" class="form-control">
                                        <%
                                        for(int i=0; i<departmentList.size(); i++) {
                                        %>
                                        <option><%=departmentList.get(i)%></option>
                                        <% } %>
                                    </select>
                                    <br>
                                    <a href="index.jsp"><button type="submit" class="btn btn-default">Login</button></a>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
        </div>
    </form>
    </body>
</html>